#ifndef OPENGLWIDGET_H
#define OPENGLWIDGET_H

#include "geometryengine.h"
#include <QOpenGLWidget>
#include <QOpenGLBuffer>
#include <QOpenGLWidget>
#include <QOpenGLShader>
#include <QOpenGLShaderProgram>
#include <QMenu>
#include <QDialog>
#include <QLabel>
#include <QComboBox>
#include <QSpinBox>
#include <QCheckBox>
#include "customdata.h"
#include <QHash>

class OpenGLWidget:public QOpenGLWidget,protected QOpenGLFunctions
{
    Q_OBJECT
public:
    OpenGLWidget(QWidget* parent=0);
    ~OpenGLWidget();
    void AddData(CustomData data);
    void AddMultiData(QList<CustomData> *lstData);
    void triggerAnimation(bool isStop);

protected:
    void initializeGL();
    void paintGL();
    void resizeGL(int w, int h);

    void mousePressEvent(QMouseEvent *e) override;
    void mouseMoveEvent(QMouseEvent *event) override;
    void wheelEvent(QWheelEvent *event) override;
    void contextMenuEvent(QContextMenuEvent *event) override;
public slots:
    void cbBoxFEMParamsChanged(int currentIndex);
    void saveAnimation();
private slots:
    void showSetFigureDialog();
    void setColor(int index);
    void setOptionEnable(bool enable);
    void setMapLevels(int levels);

private:
    void createActions();
    void createDialog();
    void initShaders();
    void initTextures();
    void displayAxis();
    void displayGrid();
    void displayLegend();
    void fillColorList(QComboBox *cbBox);
    void DisplayAnimation();
    void setWellAlpha(int alpha);

    QOpenGLTexture *textureWell;
    QOpenGLTexture *textureString;
    QOpenGLShader *vShader;
    QOpenGLShader *fShader;
    QOpenGLShaderProgram *shaderProgram;
    QOpenGLBuffer vbo;

    QVector2D mousePressPos;
    QQuaternion rotation;
    QMatrix4x4 projection;

    QMatrix4x4 m_matS;

    QColor m_Background;
    QColor m_ForeColor;
    //QColor m_StringColor;
    //QColor m_WellColor;

    bool IsDisplayAxis;
    bool IsDisplayGrid;
    bool IsDisplayString;
    bool IsDisplayWell;
    bool isDisplayLegend;

    quint16 xGridNum =5;
    quint16 yGridNum = 5;
    quint16 zGridNum = 10;
    float xMin,xMax,yMin,yMax,zMin,zMax;

    QList<CustomData> *m_LstCustomData;
    QList<QImage> *m_lstImageData;
    bool isGetImageData;

    CustomData m_CurrentData;

    GeometryEngine *stringGeometryEngine;
    GeometryEngine *wellGeometryEngine;
    QWidget *colorW;
    QLabel *labelColor;
    QMenu *popMenu;
    QAction *setFigureAction;
    QDialog *viewDialog;
    QLabel *lbBackColor;
    QLabel *lbForeColor;
    //QLabel *lbStringColor;
    //QLabel *lbWellColor;
    QLabel *lbWellMapLevels;
    QLabel *lbWellMapMin;
    QLabel *lbWellMapMax;
//    QLabel *lbFEMMapLevels;
//    QLabel *lbFEMlMapMin;
//    QLabel *lbFEMMapMax;
//    QLabel *lbFEMMapParams;
    QLabel *lbStringMapLevels;
    QLabel *lbStringMapMin;
    QLabel *lbStringMapMax;
    QLabel *lbWellMapAplha;
    QComboBox *cbBoxBackColor;
    //QComboBox *cbBoxStringColor;
    QComboBox *cbBoxWellColor;
    QComboBox *cbBoxForeColor;
    QCheckBox *ckBoxAxis;
    QCheckBox *ckBoxGrid;
    QCheckBox *ckBoxString;
    QCheckBox *ckBoxWell;
    QCheckBox *ckBoxLegend;
    QSpinBox *spinBoxWellLevels;
    QSlider *sliderWellAlpha;
    QComboBox *cbBoxWellMapMin;
    QComboBox *cbBoxWellMapMax;
    QComboBox *cbBoxStringMapMin;
    QComboBox *cbBoxStringMapMax;
    QSpinBox *spinBoxStringLevels;
//    QComboBox *cbBoxFEMMapMin;
//    QComboBox *cbBoxFEMMapMax;
//    QSpinBox *spinBoxFEMLevels;
//    QComboBox *cbBoxFEMParams;

    QTimer *timer;
    int mCurIndex = 0;
    QList<float> *lstRange;

    ColorMap mWellColorMap;
//    ColorMap mStringColorMap;
    int colorLocation;
    float wellColorAlpha=0.5;

    QHash<QString,ColorMap> mFEMColorMap;
    QString m_CurrentStringParam;
};

#endif // OPENGLWIDGET_H
